package com.web.biz.dlzc.ctrl;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.controller.BaseDomainCtrl;
import com.css.sword.platform.web.controller.annotations.CTRL;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.css.sword.platform.web.fileupload.intimeup.SwordFileItem;
import com.web.common.utils.FileUploadUtiles;
import com.web.common.utils.LoginInfoUtil;
import com.web.common.utils.StringUtil;


@CTRL("LoginRegisterCtrl")
public class LoginRegisterCtrl extends BaseDomainCtrl {

	private static SwordLogUtils log = SwordLogUtils.getLogger(LoginRegisterCtrl.class);
	
	/**
	 * 输入用户名密码登录系统
	 */
	public IResData login(IReqData req) throws Exception{
		log.debug("开始进入 LoginRegisterCtrl_login 方法。");
		Object yzm = req.getForm("form_denglu").get("yzm");
		HttpServletRequest hreq = this.getHttpReq(); 
		HttpSession session = hreq.getSession(true);
		//信访件查询码修改
		Object imgyzm = session.getAttribute("valcode");
		IResData res = new SwordRes();
		String msg = "";
		if (imgyzm!=null&&yzm!=null&&yzm.equals(imgyzm)) {
			res = (IResData) SwordServiceUtils.callLocalService("LoginRegisterService_login", req);
			Object yhm = res.get("yhm");
			if(!"".equals(StringUtil.parse2String(yhm))){
				LoginInfoUtil.register(hreq, "yhm", yhm);
			}else {
				msg = "用户名或密码错误！";
			}
		}else {
			msg = "验证码错误！";		
		}
		res.addAttr("msg", msg);
		return res;
	}

	/**
	 * 注销
	 */
	public IResData logout(IReqData req) throws Exception{

		log.debug("开始进入 LoginRegisterCtrl_logout 方法。");
		//清空session
		HttpServletRequest hreq = this.getHttpReq(); 
		HttpSession session = hreq.getSession(true);
		session.removeAttribute("yhm");
		IResData res = new SwordRes();
		//res = logout2Login(req);
		//res = (IResData) SwordServiceUtils.callLocalService("ToLoginService_init", req);
		//初始化首页数据失败
		res.addAttr("username", "");
		res.addPage("/pages/lsh/index.html");
		return res;
	}


	/**
	 * 注册
	 * register
	 */
	public IResData register(IReqData req) throws Exception{

		log.debug("开始进入 LoginRegisterCtrl_register 方法。");
		Object yzm = req.getForm("form_register").get("yzm");
		HttpServletRequest hreq = this.getHttpReq(); 
		HttpSession session = hreq.getSession(true);
		//修改验证码
		Object imgyzm = session.getAttribute("valcode");
		IResData res = new SwordRes();
		String flag = "";
		if (imgyzm!=null&&yzm!=null&&yzm.equals(imgyzm)) {
			//获取前台的图标文件
			List<SwordFileItem> fjlist1 = this.getFormUploadList2(req, "form_register", "tbfj");
			String tbfj = FileUploadUtiles.uploadFileAndGetFileXML(fjlist1,FileUploadUtiles.DOMAIN_SHOPLOGO);
			req.setBizParams("tbfj", tbfj);
			//获取前台的凭证文件
			List<SwordFileItem> fjlist2 = this.getFormUploadList2(req, "form_register", "pzfj");
			String pzfj = FileUploadUtiles.uploadFileAndGetFileXML(fjlist2,FileUploadUtiles.DOMAIN_SHOPXXSH);
			req.setBizParams("pzfj", pzfj);
			res = (IResData) SwordServiceUtils.callLocalService("LoginRegisterService_saveRegister", req);
			flag = "true";
		}else {
			res.addMessage("验证码不正确！");
		}
		res.addAttr("flag", flag);
		return res;

	}

	/**
	 * 跳转到登录页(首页)
	 */
	public IResData toLogin(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterCtrl_toLogin 方法。");
		IResData res = new SwordRes();
		//初始化首页数据开始
		res = (IResData) SwordServiceUtils.callLocalService("ToLoginService_init", req);
		//初始化首页数据失败
		String username = LoginInfoUtil.getLoginUserName();
		res.addAttr("username", username);
		res.addPage("/pages/web/login/login.jsp");
		return res;
	}
	
	/**
	 * 退出到登录页(首页)
	 */
	public IResData logout2Login(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterCtrl_logout2Login 方法。");
		IResData res = new SwordRes();
		//初始化首页数据开始
		res = (IResData) SwordServiceUtils.callLocalService("ToLoginService_init", req);
		//初始化首页数据失败
		res.addAttr("username", "");
		res.addPage("/pages/web/login/login.jsp");
		return res;
	}
	
	/**
	 * 跳转注册页面
	 */
	public IResData toRegister(IReqData req) throws Exception {
		log.debug("开始进入 LoginRegisterCtrl_toRegister 方法。");
		IResData res = new SwordRes();
		res = (IResData) SwordServiceUtils.callService("LoginRegisterService_queryTcategoryxx", req);
		res.addPage("pages/web/register/registers.jsp");
		return res;
	}
}